package com.leo.component;

import com.almasb.fxgl.core.collection.PropertyMap;
import com.almasb.fxgl.dsl.FXGL;
import com.almasb.fxgl.entity.component.Component;
import com.almasb.fxgl.texture.AnimatedTexture;
import com.almasb.fxgl.texture.AnimationChannel;
import javafx.util.Duration;

/**
 * @Author: wangss
 * @CreateTime: 2023-06-06  19:11
 * @Description: 组合组件
 */
public class CombinationComponent extends Component {

    private AnimatedTexture pAt;
    private AnimatedTexture at;

    @Override
    public void onAdded() {
        AnimationChannel ac = new AnimationChannel(FXGL.image("组合/翅膀1.png"), Duration.seconds(1), 12);
        at = new AnimatedTexture(ac);
        at.loop();
        entity.getViewComponent().addChild(at);

        AnimationChannel pAc = new AnimationChannel(FXGL.image("组合/火舞.png"), Duration.seconds(1), 16);
        pAt = new AnimatedTexture(pAc);
        pAt.setTranslateX(100);
        pAt.setTranslateY(100);
        pAt.loop();
        entity.getViewComponent().addChild(pAt);

        entity.getViewComponent().addOnClickHandler(mouseEvent -> {
            setWing("data/starWing.kv");
        });

    }

    public void setWing(String filePath) {
        entity.getViewComponent().clearChildren();
        PropertyMap propertyMap = FXGL.getAssetLoader().loadPropertyMap(filePath);
        String image = propertyMap.getString("image");
        int duration = propertyMap.getInt("duration");
        int numFrame = propertyMap.getInt("numFrame");
        int tx = propertyMap.getInt("tx");
        int ty = propertyMap.getInt("ty");

        AnimationChannel attackAc = new AnimationChannel(FXGL.image(image), Duration.seconds(duration), numFrame);
        AnimatedTexture attackAt = new AnimatedTexture(attackAc);
        attackAt.setTranslateX(tx);
        attackAt.setTranslateY(ty);
        attackAt.loop();
        entity.getViewComponent().addChild(attackAt);
        entity.getViewComponent().addChild(at);
        entity.getViewComponent().addChild(pAt);
    }
}
